package com.dao.impl;

import static com.def.GenericDef.E_XPWOTOOL_NOT_FOUND_RECORDER;
import static com.def.GenericDef.E_XPWOTOOL_RECORDER_ALREADY_EXIST;
import static com.def.GenericDef.RETURN_CODE_OK;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.dao.IRetWoToolDao;
import com.model.Ret_wo_tool;
import com.tx.Xpwotool.XpwotoolI;
import com.tx.Xpwotool.XpwotoolIA;
import com.tx.Xpwotool.XpwotoolO;
import com.tx.Xpwotool.XpwotoolOA;
import com.util.GUIDGenerator;
import com.util.Util;
 
@Repository
public class BisWoToolHibernateDao extends GenericHibernateDao<Ret_wo_tool, String> 
implements IRetWoToolDao
{
	public XpwotoolO queryByCondition(XpwotoolI xpbismtlI){
		/***
		 * 支持
		 *   
		 *   by bay_id,wo_id_fk,ope_id_fk 查询
		 *   
		 * **/
		 
		XpwotoolO xpwotoolO = new XpwotoolO();
		xpwotoolO.setTrx_id("XPWOTOOL");
		xpwotoolO.setType_id("O");
		List<XpwotoolOA> oaryList = new ArrayList<XpwotoolOA> ();
		
		XpwotoolIA iary = xpbismtlI.getIary().get(0);
		String bay_id = iary.getBay_id();
		String wo_id_fk = iary.getWo_id_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		
		String hql ="FROM Ret_wo_tool where 1=1 ";
		if( bay_id != null){
			hql = hql + " bay_id ='" + bay_id + "'" ;
		}
		if( wo_id_fk !=null){
			if( wo_id_fk.indexOf('*')==-1){
				hql = hql + " and wo_id_fk='" + wo_id_fk + "'";
			}else{
				hql = hql + " and wo_id_fk like '" + wo_id_fk.replace('*', '%') + "'";
			}
			hql = hql + " wo_id_fk ='"+ wo_id_fk +"'" ;
			
		}
		if( ope_id_fk !=null){
			hql = hql + " ope_id_fk ='" + ope_id_fk +"'" ;
		}
		
		List<Ret_wo_tool> retWOToolList =this.find( hql );
		for(Ret_wo_tool ret_wo_tool : retWOToolList){
			XpwotoolOA oary = new XpwotoolOA();
			oary.setOpe_id_fk(ret_wo_tool.getOpe_id_fk());
			oary.setBay_id(ret_wo_tool.getBay_id());
			oary.setWo_id_fk(ret_wo_tool.getWo_id_fk());
			oary.setEff_flg(ret_wo_tool.getEff_flg());
			oary.setEff_timestamp(ret_wo_tool.getEvt_timestamp());
			oary.setValid_usr(ret_wo_tool.getValid_usr());
			oary.setInval_usr(ret_wo_tool.getInval_usr());
			oary.setEvt_timestamp(ret_wo_tool.getEvt_timestamp());
			oaryList.add(oary);
		}
		xpwotoolO.setOary(oaryList);
		xpwotoolO.setTbl_cnt(oaryList.size());
		xpwotoolO.setRtn_code(RETURN_CODE_OK);
		xpwotoolO.setRtn_mesg("success");
		return xpwotoolO;
	}
	
	public XpwotoolO addProc(XpwotoolI xpwotoolI){
		XpwotoolO xpwotoolO = new XpwotoolO();
		xpwotoolO.setTrx_id("XPWOTOOL");
		xpwotoolO.setType_id("O");
		
		XpwotoolIA iary = xpwotoolI.getIary().get(0);
		String bay_id = iary.getBay_id();
		String wo_id_fk = iary.getWo_id_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		String hql = "FROM Ret_wo_tool where ope_id_fk =:ope_id_fk ";
		hql = hql + " wo_id_fk =:wo_id_fk and bay_id=:bay_id ";
		
		Ret_wo_tool queryRetWoTool =getDataByKeyProc(bay_id,wo_id_fk,ope_id_fk);
		if( queryRetWoTool != null){
			xpwotoolO.setRtn_code(E_XPWOTOOL_RECORDER_ALREADY_EXIST+"");
			xpwotoolO.setRtn_mesg("Record already Exist");
			return xpwotoolO;
		}
		Ret_wo_tool ret_wo_tool = new Ret_wo_tool();
		ret_wo_tool.setOpe_id_fk(ope_id_fk);
		ret_wo_tool.setBay_id(bay_id);
		ret_wo_tool.setWo_id_fk(wo_id_fk);
		ret_wo_tool.setEff_flg(iary.getEff_flg());
		ret_wo_tool.setEff_timestamp(iary.getEff_timestamp());
		ret_wo_tool.setValid_usr(iary.getValid_usr());
		ret_wo_tool.setInval_usr(iary.getInval_usr());
		ret_wo_tool.setEvt_timestamp(Util.getCurrentTimestamp());
        this.save(ret_wo_tool);
        
        xpwotoolO.setRtn_code(RETURN_CODE_OK);
        xpwotoolO.setRtn_mesg("success");
		return xpwotoolO;
	}
	public XpwotoolO updateProc(XpwotoolI xpwotoolI){
		XpwotoolO xpwotoolO = new XpwotoolO();
		xpwotoolO.setTrx_id("XPWOTOOL");
		xpwotoolO.setType_id("O");
		
		XpwotoolIA iary = xpwotoolI.getIary().get(0);
		String bay_id = iary.getBay_id();
		String wo_id_fk = iary.getWo_id_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		
		Ret_wo_tool retWoTool =getDataByKeyProc(bay_id,wo_id_fk,ope_id_fk);
		if( retWoTool != null){
			xpwotoolO.setRtn_code(E_XPWOTOOL_NOT_FOUND_RECORDER+"");
			xpwotoolO.setRtn_mesg("not found record");
			return xpwotoolO;
		}
		//获取主键
		retWoTool.setUnq_seq_id(GUIDGenerator.javaGUID());
		if(iary.getEff_flg()!=null){
			retWoTool.setEff_flg(iary.getEff_flg());
		}
		if(iary.getEff_timestamp()!=null){
			retWoTool.setEff_timestamp(iary.getEff_timestamp());
		}
		if(iary.getValid_usr()!=null){
			retWoTool.setValid_usr(iary.getValid_usr());
		}
		if(iary.getInval_usr()!=null){
			retWoTool.setInval_usr(iary.getInval_usr());
		}
		retWoTool.setEvt_timestamp(Util.getCurrentTimestamp());
        this.update(retWoTool);
        
        xpwotoolO.setRtn_code(RETURN_CODE_OK);
        xpwotoolO.setRtn_mesg("success");
		return xpwotoolO;
	}
	public XpwotoolO deleteProc(XpwotoolI xpwotoolI){
		XpwotoolO xpwotoolO = new XpwotoolO();
		xpwotoolO.setTrx_id("XPWOTOOL");
		xpwotoolO.setType_id("O");
		XpwotoolIA iary = xpwotoolI.getIary().get(0);
		
		String bay_id = iary.getBay_id();
		String wo_id_fk = iary.getWo_id_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		
		Ret_wo_tool retWoTool =getDataByKeyProc(bay_id,wo_id_fk,ope_id_fk);
		if( retWoTool != null){
			xpwotoolO.setRtn_code(E_XPWOTOOL_NOT_FOUND_RECORDER+"");
			xpwotoolO.setRtn_mesg("not found record");
			return xpwotoolO;
		}
		
    	this.delete(retWoTool);
		

    	xpwotoolO.setRtn_code(RETURN_CODE_OK);
    	xpwotoolO.setRtn_mesg("success");
		return xpwotoolO;
	}
	public Ret_wo_tool getDataByKeyProc(String ope_id_fk,String bay_id,String wo_id_fk){
		String hql = "FROM Ret_wo_tool where ope_id_fk =:ope_id_fk ";
		hql = hql + " wo_id_fk =:wo_id_fk and bay_id=:bay_id ";
		Query query = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		query.setString("ope_id_fk", ope_id_fk);
		query.setString("wo_id_fk", wo_id_fk);
		query.setString("bay_id", bay_id);
		query.setString("ope_id_fk", ope_id_fk);
		query.setString("wo_id_fk", wo_id_fk);
		query.setString("bay_id", bay_id);
		
		Ret_wo_tool retWoTool =(Ret_wo_tool) query.uniqueResult();
		return retWoTool;
	}
	
}
